﻿using log4net;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTu.Logging
{
    /// <summary>
    /// log4net 创建工厂
    /// </summary>
    public class BaiTuLog4netFactory : ILogging.ILoggerFactory
    {      
        private static bool _isFileWatched = false;

        public BaiTuLog4netFactory()
            : this("config/log4net.config") { }

        public BaiTuLog4netFactory(string configFilename)
        {
            if (!_isFileWatched && !string.IsNullOrWhiteSpace(configFilename) &&AppDomain.CurrentDomain.IsFullyTrusted) {
                // Only monitor configuration file in full trust
                XmlConfigurator.ConfigureAndWatch(Common.Helpers.FileHelper.GetFileInfo(Common.Web.Helpers.MapPathHelper.GetPhyicalPath(configFilename)));
                _isFileWatched = true;
            }
        }

        public ILogging.ILogger CreateLogger(Type type)
        {
            return new BaiTuLog4netLogger(LogManager.GetLogger(type), this);
        }


        public ILogging.ILogger CreateLogger(string name)
        {
            return new BaiTuLog4netLogger(LogManager.GetLogger(name), this);
        }
    }
}
